package mvc.tubolar;

import java.awt.Dimension;

import javax.media.opengl.GL;

import mvc.ControllerMouse3D;

import org.geom.Triangle;
import org.geom.render.ViewJOGL;

public class TubolarView3D extends ViewJOGL {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private TubolarModel tubeModel;

	public TubolarView3D(TubolarModel model, Dimension d) {
		super(d);

		this.tubeModel = model;

	}

	@Override
	public void render(GL gl) {
		//System.out.println("TubolarView3D > render");
		lights(gl);

		this.drawCenter(gl);
		drawCurve(tubeModel.getTrunk().getPoints(), gl);		
		drawSurface(gl);
	}

	private void drawSurface(GL gl) {
		gl.glColor3f(1f,0f,0f);
		gl.glBegin(GL.GL_TRIANGLES);
		for(Triangle t : tubeModel.getTriangles()) {
			gl.glVertex3f(t.v1.x, t.v1.y, t.v1.z);
			gl.glVertex3f(t.v2.x, t.v2.y, t.v2.z);
			gl.glVertex3f(t.v3.x, t.v3.y, t.v3.z);
		}
		gl.glEnd();

		// if the curve is a closed curve..
		// last one!
		//gl.glColor3f(255, 0, 0);
		//drawCircles(tube.size-1, 0, gl);
	}

	private void lights(GL gl) {
		float[] LightPos = {1.0f, 1.0f, 0f, 1.0f};
		float[] LightAmb = {0.2f, 0.2f, 0.2f, 1.0f};
		float[] LightDif = {1f, 1f, 1f, 1.0f};
		float[] LightSpc = {0.9f, 0.9f, 0.9f, 1.0f};

		//gl.glEnable(GL.GL_COLOR_MATERIAL);
		//gl.glColorMaterial(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE);
		
		//float specReflection[] = { 0.8f, 0.8f, 0.8f, 1.0f };
		//gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, specReflection, 0);
		//gl.glMateriali(GL.GL_FRONT, GL.GL_SHININESS, 56);
		
		gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, LightPos, 0);
		gl.glLightfv(GL.GL_LIGHT1, GL.GL_AMBIENT, LightAmb, 0);
		gl.glLightfv(GL.GL_LIGHT1, GL.GL_DIFFUSE, LightDif, 0);
		
		//gl.glLightfv(GL.GL_LIGHT1, GL.GL_SPECULAR, LightSpc, 0);

		float[] LightDif2 = {0f, 0f, -1f, 1.0f};
		//gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, LightDif2, 0);
		//gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, LightSpc, 0);

		gl.glEnable(GL.GL_LIGHT0);
		//gl.glEnable(GL.GL_LIGHT1);
		gl.glShadeModel(GL.GL_SMOOTH);
		gl.glEnable(GL.GL_LIGHTING);  
	}

	public void addMouseKeybController(ControllerMouse3D controller3d) {
		addMouseListener(controller3d);
		addMouseMotionListener(controller3d);
		addKeyListener(controller3d);
	}




}
